﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.PostModel;
using WebApi.Models;
using WebApi.Class;
using Yd.Domain.DAL;
using Yd.BLL;
using System.Data;
using System.Data.SqlClient;
using Yd.Model;
using System.Text;
namespace WebApi.Controllers
{
    public class ActionController : ApiController
    {
        public CommDAL commdal = new CommDAL();
        /// Post 獲取列表 api/user/shopList
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object ActionList([FromBody]PostShopsList post)
        {
            ModelBase mbase = new ModelBase();
            string pagewhere = " isDel=0 ";
            if (post.pageSize == 0)
            {
                post.pageSize = 10;
            }
            DataSet newlist = commdal.QueryObjectAllSql("declare @t int Execute Proce_Sql_PageChange 'view_actionlist','  * ','hdId desc','" + pagewhere + "'," + post.pageSize + "," + post.pageIndex + ",@t output");
            if (newlist != null)
            {
                mbase.code = 0;
                mbase.data = newlist.Tables[0];
                mbase.message = "获取成功";
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
            }
            return mbase;
        }
        // Post 获取活动详情 api/users/PostUserInfo
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object PostActivityInfo([FromBody]PostActivtyAboutByHdid post)
        {
            ModelBase mbase = new ModelBase();
            if (post.hdid == 0)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            string sql = String.Format(@"SELECT [hdId]
      ,[actionType]
      ,[shopid]
      ,[actionName]
      ,('http://www.11fifteen.cn/'+[actionImgUrl]) as actionImgUrl
      ,(SELECT CONVERT(VARCHAR(100), createTime, 20)) as createTime
      ,[actionContent]
      ,[actionAddress]
      ,(SELECT CONVERT(VARCHAR(100), startTime, 20)) as startTime
      ,(SELECT CONVERT(VARCHAR(100), endTime, 20)) as endTime
      ,[ticketInformation]
  FROM [dbo].[yd_action] where hdId={0}", post.hdid);
            DataSet dsuser = commdal.QueryObjectAllSql(sql);
            if (dsuser != null && dsuser.Tables[0].Rows.Count > 0)
            {
                ActivityinfoModel model = ActivityinfoModel.GetActivity(dsuser.Tables[0].Rows[0]);
                if (model != null)
                {
                    mbase.code = 0;
                    mbase.data = model;
                    mbase.message = ErrorCodeUtil.getCodeMessage(0);
                }
                else
                {
                    mbase.code = -1;
                    mbase.message = ErrorCodeUtil.getCodeMessage(-1);
                }
            }
            else
            {
                mbase.code = -20;
                mbase.message = ErrorCodeUtil.getCodeMessage(-20);
            }
            return mbase;
        }
        //获取商家添加的活动列表
        public object ShopActionList([FromBody]PostActionList post)
        {
            ModelBase mbase = new ModelBase();
            if (post.shopid <1)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            string pagewhere = " shopid=" + post.shopid;
            if (post.pageSize == 0)
            {
                post.pageSize = 10;
            }
            DataSet newlist = commdal.QueryObjectAllSql("declare @t int Execute Proce_Sql_PageChange 'view_actionlist','  * ','hdId desc','" + pagewhere + "'," + post.pageSize + "," + post.pageIndex + ",@t output");
            if (newlist != null)
            {
                mbase.code = 0;
                mbase.data = newlist.Tables[0];
                mbase.message = "获取成功";
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
            }
            return mbase;
        }
        /// Post 獲取列表 api/user/shopList
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object ActionTickList([FromBody]PostActivtyAboutByHdid post)
        {
            ModelBase mbase = new ModelBase();
            DataSet newlist = commdal.Gettz_tick(post.hdid.ToString());
            if (newlist != null)
            {
                mbase.code = 0;
                mbase.data = newlist.Tables[0];
                mbase.message = "获取成功";
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
            }
            return mbase;
        }
        // Post 获取活动票详情 api/Action/PostTicketInfo
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object PostTicketInfo([FromBody]PostTicketInfoModel post)
        {
            ModelBase mbase = new ModelBase();
            if (post.ticketId== 0)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            string sql = String.Format(@"SELECT [tid]
      ,[hdId]
      ,[ticketName]
      ,[fare]
      ,[endTime]
      ,[allVotes]
  FROM [dbo].[yd_ticket] where tid={0}", post.ticketId);
            DataSet dsuser = commdal.QueryObjectAllSql(sql);
            if (dsuser != null && dsuser.Tables[0].Rows.Count > 0)
            {
                TicketModel model = TicketModel.GetActivity(dsuser.Tables[0].Rows[0]);
                if (model != null)
                {
                    mbase.code = 0;
                    mbase.data = model;
                    mbase.message = ErrorCodeUtil.getCodeMessage(0);
                }
                else
                {
                    mbase.code = -1;
                    mbase.message = ErrorCodeUtil.getCodeMessage(-1);
                }
            }
            else
            {
                mbase.code = -20;
                mbase.message = ErrorCodeUtil.getCodeMessage(-20);
            }
            return mbase;
        }
        //添加活动
        public object InsertAction([FromBody]PostSubmitAction post)
        {
            ModelBase mbase = new ModelBase();
            if (post.shopid < 1 || StringUtils.getIsEmpty(post.actionName) || StringUtils.getIsEmpty(post.actionAddress) || StringUtils.getIsEmpty(post.actionContent) || StringUtils.getIsEmpty(post.actionImgUrl) || StringUtils.getIsEmpty(post.actionCost))
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            int _fresult = -1;
            yd_action fnews = new yd_action();
            fnews.actionName = post.actionName.Trim();
            fnews.actionContent = post.actionContent.Trim();
            fnews.actionAddress = post.actionAddress.Trim();
            fnews.startTime = post.startTime.Trim();
            fnews.endTime = post.endTime;
            fnews.actionImgUrl = post.actionImgUrl;
            if (!StringUtils.IsNumeric(post.actionCost))
            {
                mbase.code = -1;
                mbase.message = "请输入正确金额";
                return mbase;
            }
            decimal feiyong = 0;
            decimal.TryParse(post.actionCost, out feiyong);
            fnews.actionCost = feiyong;
            fnews.actionType = 0;
            fnews.shopid = post.shopid;
            _fresult = commdal.InstallAction(fnews);
            if (_fresult == 0)
            {
                mbase.code = 0;
                mbase.message = "添加成功";
                return mbase;
            }
            mbase.code = -1;
            mbase.message = "操作失败";
            return mbase;
        }

        // Post 商家编辑活动信息
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object EditShopActionInfo([FromBody]PostEditAction post)
        {
            ModelBase mbase = new ModelBase();
            if (post.hdId < 1 || (StringUtils.getIsEmpty(post.actionName) && StringUtils.getIsEmpty(post.actionAddress) && StringUtils.getIsEmpty(post.actionContent) && StringUtils.getIsEmpty(post.actionImgUrl) && StringUtils.getIsEmpty(post.actionCost)))
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }

            StringBuilder sql = new StringBuilder();
            sql.Append("update yd_action set ");
            if (!StringUtils.getIsEmpty(post.actionName))
            {
                sql.Append(" actionName='" + post.actionName + "',");
            }
            if (!StringUtils.getIsEmpty(post.actionImgUrl))
            {
                sql.Append(" actionImgUrl='" + post.actionImgUrl + "',");
            }
            if (!StringUtils.getIsEmpty(post.actionContent))
            {
                sql.Append(" actionContent='" + post.actionContent + "',");
            }
            if (!StringUtils.getIsEmpty(post.actionAddress))
            {
                sql.Append(" actionAddress='" + post.actionAddress + "',");
            }
            if (!StringUtils.getIsEmpty(post.actionCost))
            {
                sql.Append(" actionCost=" + post.actionCost + ",");
            }
            if (!StringUtils.getIsEmpty(post.startTime))
            {
                sql.Append(" startTime=" + post.startTime + ",");
            }
            if (!StringUtils.getIsEmpty(post.endTime))
            {
                sql.Append(" endTime=" + post.endTime + ",");
            }
            sql.Remove(sql.Length - 1, 1);
            sql.Append(" where hdId=" + post.hdId);
            int _fresult = -1;
            _fresult = commdal.SaveOrUpdateSql(sql.ToString());

            if (_fresult == 0)
            {
                mbase.code = 0;
                mbase.message = "修改成功";
                return mbase;
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
            }
            return mbase;
        }
        // Post 商家删除活动信息
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object DelShopAction([FromBody]PostEditAction post)
        {
            ModelBase mbase = new ModelBase();
            if (post.hdId < 1)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }

            StringBuilder sql = new StringBuilder();
            sql.Append("update yd_action set isDel=1");
            sql.Append(" where hdId=" + post.hdId);
            int _fresult = -1;
            _fresult = commdal.SaveOrUpdateSql(sql.ToString());

            if (_fresult == 0)
            {
                mbase.code = 0;
                mbase.message = "删除成功";
                return mbase;
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
            }
            return mbase;
        }
    }
}
